package org.jeecg.modules.yc.web.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctg.ag.sdk.biz.aep_device_management.CreateDeviceResponse;
import com.ctg.ag.sdk.biz.aep_device_management.DeleteDeviceResponse;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.yc.NB.Services.NBCommandService;
import org.jeecg.modules.yc.NB.util.NBCommandUtil;
import org.jeecg.modules.yc.web.entity.Terminal;
import org.jeecg.modules.yc.web.mapper.TerminalMapper;
import org.jeecg.modules.yc.web.service.ITerminalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Description: 采集器
 * @Author: jeecg-boot
 * @Date:   2023-12-25
 * @Version: V1.0
 */
@Service
public class TerminalServiceImpl extends ServiceImpl<TerminalMapper, Terminal> implements ITerminalService {

    @Autowired
    private NBCommandService nbCommandService;
    @Autowired
    private TerminalMapper terminalMapper;

    /**
     * 修改设备信息，同时更改NB的设备
     * @param terminal
     */
    @Override
    public void edit_NB(Terminal terminal) {
        //判断是否更改IMEI号，如果更改则需要从新创建NB设备，并将老设备删除
        Terminal oldTerminal = terminalMapper.selectById(terminal.getId());
        if(terminal.getImei().equals(oldTerminal.getImei())){//没有更改
            this.updateById(terminal);
            return;
        }
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        try {
            //删除老设备
            DeleteDeviceResponse deleteDeviceResponse = nbCommandService.deleteDevice(oldTerminal.getNo());
            if(deleteDeviceResponse.getStatusCode() != 200){
                throw new Exception(deleteDeviceResponse.getMessage());
            }
            //增加新设备
            CreateDeviceResponse createDeviceResponse = nbCommandService.CreateDevice(NBCommandService.masterKey,
                    NBCommandUtil.createDevice(terminal.getName(),sysUser.getUsername(),0,terminal.getImei()));
            if(createDeviceResponse.getStatusCode() != 200){
                throw new Exception(createDeviceResponse.getMessage());
            }

            ObjectMapper mapper = new ObjectMapper();
            JsonNode rootNode = mapper.readTree(createDeviceResponse.getBody());
            if(rootNode.get("code").asInt() != 0){
                throw new Exception(rootNode.get("msg").asText());
            }
            JsonNode resultNode = rootNode.get("result");
            terminal.setNo(resultNode.get("deviceId").asText());
            terminal.setTenantId(resultNode.get("tenantId").asText());
            terminal.setProductId(resultNode.get("productId").asText());
            this.updateById(terminal);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public IPage<Terminal> selectPageVo(Page<Terminal> page, Terminal terminal,String sys_org_code) {

        return terminalMapper.selectPageVo(page,terminal,sys_org_code);
    }

    @Override
    public List<String> selectOrgCode() {
        return terminalMapper.selectOrgCode();
    }
}
